package com.burke.gwt.client;

import java.util.Date;

import com.google.gwt.canvas.dom.client.Context2d;

public class Clock {

	protected final FloaterGroup floaterGroup;
	private final Date stop;
	private long days;
	private long hours;
	private long mins;
	private long secs;
	private long mils;
	protected int x;
	protected int y;

	private Digit day1;
	private Digit day2;
	private Digit day3;
	private Digit hour1;
	private Digit hour2;
	private Digit min1;
	private Digit min2;
	private Digit sec1;
	private Digit sec2;
	
	public Clock(Date stop, FloaterGroup floaterGroup) {
		this.stop = stop;
		this.floaterGroup = floaterGroup;
		//create the digits
		day1 = new Digit(0,0,this);
		day2 = new Digit(100,0,this);
		day3 = new Digit(200,0,this);
		
		hour1 = new Digit(350,0,this);
		hour2 = new Digit(450,0,this);	
		
		min1 = new Digit(600,0,this);
		min2 = new Digit(700,0,this);	
		
		sec1 = new Digit(850,0,this);
		sec2 = new Digit(950,0,this);			
	}
	
	public void init() {
		
	}
	
	public void update(int x, int y) {
		
		this.x = x;
		this.y = y;
		
		//calculate the difference in days
		long difference = (stop.getTime() - new Date().getTime());
		   
        this.mils = difference % 1000;
        difference = (difference - mils) / 1000;
        this.secs = difference % 60;
        difference = (difference - secs) / 60;
        this.mins = difference % 60;
        difference = (difference - mins) / 60;
        this.hours = difference % 24;
        difference = (difference - hours) / 24;
        this.days = difference;

	}
	
	void draw(Context2d context) {
		context.save();
		context.translate(x, y);
		
		//draw first day digit
		day1.setDigit((int)Math.floor(days/100L));//(int)days%100);
		day1.draw(context);
		//draw second day digit
		day2.setDigit((int)Math.floor(days/10L));//Math.min((int)days%10, 9));
		day2.draw(context);
		//draw third day digit
		day3.setDigit((int)days%10);
		day3.draw(context);
		//draw first min digit
		hour1.setDigit((int)Math.floor(hours/10L));
		hour1.draw(context);
		//draw second min digit
		hour2.setDigit((int)hours%10);
		hour2.draw(context);	
		//draw first min digit
		min1.setDigit((int)Math.floor(mins/10L));
		min1.draw(context);
		//draw second min digit
		min2.setDigit((int)mins%10);
		min2.draw(context);		
		//draw first sec digit
		sec1.setDigit((int)Math.floor(secs/10L));
		sec1.draw(context);
		//draw second sec digit
		sec2.setDigit((int)secs%10);
		sec2.draw(context);
		
		context.restore();
	}
}
